package com.yserp.hg.service.impl;
import com.yserp.hg.service.HgOrdrServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.yserp.hg.entity.HgOrdrEntity;
import com.yserp.hg.entity.HgRdr1Entity;
import com.yserp.hg.entity.HgRdr2Entity;
import com.yserp.hg.entity.HgRdr3Entity;

import org.jeecgframework.core.util.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;


@Service("hgOrdrService")
@Transactional
public class HgOrdrServiceImpl extends CommonServiceImpl implements HgOrdrServiceI {
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((HgOrdrEntity)entity);
 	}
	
	public void addMain(HgOrdrEntity hgOrdr,
	        List<HgRdr1Entity> hgRdr1List,List<HgRdr2Entity> hgRdr2List,List<HgRdr3Entity> hgRdr3List){
			//保存主信息
			this.save(hgOrdr);

			/**保存-报关明细*/
			for(HgRdr1Entity hgRdr1:hgRdr1List){
				//外键设置
				hgRdr1.setDocentry(hgOrdr.getId());
				this.save(hgRdr1);
			}
			/**保存-报关附件*/
			for(HgRdr2Entity hgRdr2:hgRdr2List){
				//外键设置
				hgRdr2.setDocentry(hgOrdr.getId());

				this.save(hgRdr2);
			}
			/**保存-报关文件*/
			for(HgRdr3Entity hgRdr3:hgRdr3List){
				//外键设置
				hgRdr3.setDocentry(hgOrdr.getId());
				this.save(hgRdr3);
			}
			//执行新增操作配置的sql增强
 			this.doAddSql(hgOrdr);
	}

	
	public void updateMain(HgOrdrEntity hgOrdr,
	        List<HgRdr1Entity> hgRdr1List,List<HgRdr2Entity> hgRdr2List,List<HgRdr3Entity> hgRdr3List) {
		//保存主表信息
		HgOrdrEntity hgOrdrold = this.findUniqueByProperty(HgOrdrEntity.class,"id",hgOrdr.getId());

		try{
			if(!StringUtil.isEmpty(hgOrdr.getFilename0())){
				if(!hgOrdr.getFilename0().equals(hgOrdrold.getFilename0())){
					hgOrdr.setFiletype0(DateUtils.getDataString(DateUtils.datetimeFormat));
					hgOrdr.setFiletype1("Y");
				}
			}

		}catch (Exception e){

		}
		try {
			MyBeanUtils.copyBeanNotNull2Bean(hgOrdr, hgOrdrold);
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.saveOrUpdate(hgOrdrold);
		//===================================================================================
		//获取参数
		Object dOCENTRY0 = hgOrdr.getId();
		Object dOCENTRY1 = hgOrdr.getId();
		Object dOCENTRY2 = hgOrdr.getId();
		//===================================================================================
		//1.查询出数据库的明细数据-报关明细
	    String hql0 = "from HgRdr1Entity where 1 = 1 AND dOCENTRY = ? ";
	    List<HgRdr1Entity> hgRdr1OldList = this.findHql(hql0,dOCENTRY0);
		//2.筛选更新明细数据-报关明细
		if(hgRdr1List!=null&&hgRdr1List.size()>0){
		for(HgRdr1Entity oldE:hgRdr1OldList){
			boolean isUpdate = false;
				for(HgRdr1Entity sendE:hgRdr1List){
					//需要更新的明细数据-报关明细
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-报关明细
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-报关明细
			for(HgRdr1Entity hgRdr1:hgRdr1List){
				if(oConvertUtils.isEmpty(hgRdr1.getId())){
					//外键设置
					hgRdr1.setDocentry(hgOrdr.getId());
					this.save(hgRdr1);
				}
			}
		}
		//===================================================================================
		//1.查询出数据库的明细数据-报关附件
	    String hql1 = "from HgRdr2Entity where 1 = 1 AND dOCENTRY = ? ";
	    List<HgRdr2Entity> hgRdr2OldList = this.findHql(hql1,dOCENTRY1);
		//2.筛选更新明细数据-报关附件
		if(hgRdr2List!=null&&hgRdr2List.size()>0){
		for(HgRdr2Entity oldE:hgRdr2OldList){
			boolean isUpdate = false;
				for(HgRdr2Entity sendE:hgRdr2List){
					//需要更新的明细数据-报关附件
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-报关附件
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-报关附件
			for(HgRdr2Entity hgRdr2:hgRdr2List){
				if(oConvertUtils.isEmpty(hgRdr2.getId())){
					//外键设置
					hgRdr2.setDocentry(hgOrdr.getId());
					this.save(hgRdr2);
				}
			}
		}
		//===================================================================================
		//1.查询出数据库的明细数据-报关文件
	    String hql2 = "from HgRdr3Entity where 1 = 1 AND dOCENTRY = ? ";
	    List<HgRdr3Entity> hgRdr3OldList = this.findHql(hql2,dOCENTRY2);
		//2.筛选更新明细数据-报关文件
		if(hgRdr3List!=null&&hgRdr3List.size()>0){
		for(HgRdr3Entity oldE:hgRdr3OldList){
			boolean isUpdate = false;
				for(HgRdr3Entity sendE:hgRdr3List){
					//需要更新的明细数据-报关文件
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-报关文件
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-报关文件
			for(HgRdr3Entity hgRdr3:hgRdr3List){
				if(oConvertUtils.isEmpty(hgRdr3.getId())){
					//外键设置
					hgRdr3.setDocentry(hgOrdr.getId());
					this.save(hgRdr3);
				}
			}
		}
		//执行更新操作配置的sql增强
 		this.doUpdateSql(hgOrdr);
	}

	
	public void delMain(HgOrdrEntity hgOrdr) {
		//删除主表信息
		this.delete(hgOrdr);
		//===================================================================================
		//获取参数
		Object dOCENTRY0 = hgOrdr.getId();
		Object dOCENTRY1 = hgOrdr.getId();
		Object dOCENTRY2 = hgOrdr.getId();
		//===================================================================================
		//删除-报关明细
	    String hql0 = "from HgRdr1Entity where 1 = 1 AND dOCENTRY = ? ";
	    List<HgRdr1Entity> hgRdr1OldList = this.findHql(hql0,dOCENTRY0);
		this.deleteAllEntitie(hgRdr1OldList);
		//===================================================================================
		//删除-报关附件
	    String hql1 = "from HgRdr2Entity where 1 = 1 AND dOCENTRY = ? ";
	    List<HgRdr2Entity> hgRdr2OldList = this.findHql(hql1,dOCENTRY1);
		this.deleteAllEntitie(hgRdr2OldList);
		//===================================================================================
		//删除-报关文件
	    String hql2 = "from HgRdr3Entity where 1 = 1 AND dOCENTRY = ? ";
	    List<HgRdr3Entity> hgRdr3OldList = this.findHql(hql2,dOCENTRY2);
		this.deleteAllEntitie(hgRdr3OldList);
	}
	
 	
 	/**
	 * 默认按钮-sql增强-新增操作

	 * @return
	 */
 	public boolean doAddSql(HgOrdrEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作

	 * @return
	 */
 	public boolean doUpdateSql(HgOrdrEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作

	 * @return
	 */
 	public boolean doDelSql(HgOrdrEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,HgOrdrEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode()));
 		sql  = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode()));
 		sql  = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus()));
 		sql  = sql.replace("#{docnum}",String.valueOf(t.getDocnum()));
 		sql  = sql.replace("#{cardcode}",String.valueOf(t.getCardcode()));
 		sql  = sql.replace("#{cardname}",String.valueOf(t.getCardname()));
 		sql  = sql.replace("#{invcode}",String.valueOf(t.getInvcode()));
 		sql  = sql.replace("#{tdcode}",String.valueOf(t.getTdcode()));
 		sql  = sql.replace("#{plandtime}",String.valueOf(t.getPlandtime()));
 		sql  = sql.replace("#{duedate}",String.valueOf(t.getDuedate()));
 		sql  = sql.replace("#{ys}",String.valueOf(t.getYs()));
 		sql  = sql.replace("#{js}",String.valueOf(t.getJs()));
 		sql  = sql.replace("#{cyfy}",String.valueOf(t.getCyfy()));
 		sql  = sql.replace("#{gdfy}",String.valueOf(t.getGdfy()));
 		sql  = sql.replace("#{hzfy}",String.valueOf(t.getHzfy()));
 		sql  = sql.replace("#{total}",String.valueOf(t.getTotal()));
 		sql  = sql.replace("#{comment}",String.valueOf(t.getComment()));
 		sql  = sql.replace("#{jsstatus}",String.valueOf(t.getJsstatus()));
 		sql  = sql.replace("#{docstatus}",String.valueOf(t.getDocstatus()));
 		sql  = sql.replace("#{mtou}",String.valueOf(t.getMtou()));
 		sql  = sql.replace("#{pol}",String.valueOf(t.getPol()));
 		sql  = sql.replace("#{pod}",String.valueOf(t.getPod()));
 		sql  = sql.replace("#{hscode}",String.valueOf(t.getHscode()));
 		sql  = sql.replace("#{ei_type}",String.valueOf(t.getEiType()));
 		sql  = sql.replace("#{shiping}",String.valueOf(t.getShiping()));
 		sql  = sql.replace("#{tran_type}",String.valueOf(t.getTranType()));
 		sql  = sql.replace("#{fxdonum}",String.valueOf(t.getFxdonum()));
 		sql  = sql.replace("#{ifcustoms}",String.valueOf(t.getIfcustoms()));
 		sql  = sql.replace("#{qty}",String.valueOf(t.getQty()));
 		sql  = sql.replace("#{doctotal}",String.valueOf(t.getDoctotal()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}